function [ userEqtn, eqtnLabel, ...
    dynamLhs, dynamRhs, dynamSign, ...
    steadyLhs, steadyRhs, steadySign, ...
    maxSh, minSh, lsInvalidTimeSubs, lsEmptyEqtn ] = parseeqtns(blk)
% parseeqtns [Not a public function] Parse equations within an equation block.
%
% Backend IRIS function.
% No help provided.

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

import preparser.White;

% Parse the structure of individual equations.
% @@@@@ MOSW.
% Extra pair of brackets needed in Octave.
EQTN_PATTERN = [ ...
    '(("[^"]*"|''[^'']*'')?)', ... % Label.
    '([^!;]*)', ... % Dynamic (full) equation.
    '((!![^!;]*)?);', ... % Steady equation.
    ];

% Replace mulitple labels with the last one.
MULTIPLE_LABEL_PATTERN = '(("[^"]*"|''[^'']*'')\s*)+("[^"]*"|''[^'']*'')';

%--------------------------------------------------------------------------

userEqtn = cell(1,0);
eqtnLabel = cell(1,0);
dynamLhs = cell(1,0);
dynamRhs = cell(1,0);
dynamSign = cell(1,0);
steadyLhs = cell(1,0);
steadyRhs = cell(1,0);
steadySign = cell(1,0);
maxSh = 0;
minSh = 0;
lsInvalidTimeSubs = cell(1,0);
lsEmptyEqtn = cell(1,0);

% Remove line breaks.
blk = strrep(blk,sprintf('\n'),'');
blk = strrep(blk,sprintf('\r'),'');

% Split the entire block into individual equations.
whBlk = White.whiteOutLabel(blk);
[from, to] = regexp(whBlk, '[^;]+;', 'start', 'end');
nEqtn = length(from);
if nEqtn==0
    return
end
eqtn = cell(1,nEqtn);
for iEqtn = 1 : nEqtn
    eqtn{iEqtn} = blk( from(iEqtn):to(iEqtn) ); 
end

% Replace multiple labels with the last one.
eqtn = regexprep(eqtn, MULTIPLE_LABEL_PATTERN, '$2');

% Trim the equations, otherwise labels at the beginning would not be
% matched.
eqtn = strtrim(eqtn);

% Break down equations into label, dynamic, steady.
tkn = regexp(eqtn, EQTN_PATTERN, 'tokens', 'once');

if true % ##### MOSW
    % Do nothing.
else
    for i = 1 : length(tkn) %#ok<UNRCH>
        if length(tkn{i})==2
            tkn{i} = [ {''}, tknExt{i} ];
        end
    end
end

tkn = [ tkn{:} ];
eqtnLabel = tkn(1:3:end);
dynamEqtn = tkn(2:3:end);
steadyEqtn = tkn(3:3:end);

% Remove white spaces from equations but not from labels.
dynamEqtn = regexprep(dynamEqtn, '\s+', '');
steadyEqtn = regexprep(steadyEqtn, '\s+', '');

% Put together user equations after removing white spaces from equations.
userEqtn = strcat(dynamEqtn, steadyEqtn, ';');

steadyEqtn = strrep(steadyEqtn, '!!', '');
dynamEqtn = strrep(dynamEqtn, '!ttrend', 'ttrend');
steadyEqtn = strrep(steadyEqtn, '!ttrend', 'ttrend');

% Remove equations that are completely empty, no warning.
ixEmpty = ...
    cellfun(@isempty, eqtnLabel) ...
    & cellfun(@isempty, dynamEqtn) ...
    & cellfun(@isempty, steadyEqtn);
if any(ixEmpty)
    userEqtn(ixEmpty) = [ ];
    eqtnLabel(ixEmpty) = [ ];
    dynamEqtn(ixEmpty) = [ ];
    steadyEqtn(ixEmpty) = [ ];
end

% Remove equations that consist of labels only; throw a warning later.
ixEmptyEqtn = ...
    cellfun(@isempty, dynamEqtn) ...
    & cellfun(@isempty, steadyEqtn);
if any(ixEmptyEqtn)
    % Report empty equations including their labels.
    lsEmptyEqtn = strcat(eqtnLabel(ixEmptyEqtn), userEqtn(ixEmptyEqtn));
    userEqtn(ixEmptyEqtn) = [ ];
    eqtnLabel(ixEmptyEqtn) = [ ];
    dynamEqtn(ixEmptyEqtn) = [ ];
    steadyEqtn(ixEmptyEqtn) = [ ];
end

% Remove quotation marks from labels.
for i = 1 : length(eqtnLabel)
    % Make sure empty labels are '' and not [1x0 char].
    if length(eqtnLabel{i})>2
        eqtnLabel{i} = eqtnLabel{i}(2:end-1);
    end
end

% Validate and evaluate time subscripts, and get max and min shifts (these
% only need to be determined from dynamic equations).
[dynamEqtn, maxSh, minSh, dynamLsInvalid] = theparser.evaltimesubs(dynamEqtn);
[steadyEqtn, ~, ~, steadyLsInvalid] = theparser.evaltimesubs(steadyEqtn);
lsInvalidTimeSubs = [ dynamLsInvalid, steadyLsInvalid ];

% Split equations into LHS, sign, and RHS.
[dynamLhs, dynamRhs, dynamSign] = getLhsSignRhs(dynamEqtn);
[steadyLhs, steadyRhs, steadySign] = getLhsSignRhs(steadyEqtn);
end




function [lhs,rhs,sign] = getLhsSignRhs(eqtn)
nEqtn = length(eqtn);
lhs = cell(1,nEqtn);
rhs = cell(1,nEqtn);
sign = cell(1,nEqtn);
[from,to] = regexp(eqtn,':=|=#|\+=|=','once','start','end');
for i = 1 : nEqtn
    if ~isempty(from{i})
        lhs{i}  = eqtn{i}( 1:from{i}-1 );
        sign{i} = eqtn{i}( from{i}:to{i} );
        rhs{i}  = eqtn{i}( to{i}+1:end );
    else
        lhs{i}  = '';
        sign{i} = '';
        rhs{i}  = eqtn{i};
    end
end
end

